package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entity.CmfzUser;
import com.baizhi.cmfz.mapper.CmfzUserMapper;
import com.baizhi.cmfz.service.CmfzUserService;
import com.baizhi.cmfz.util.ThreadPollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Transactional
public class CmfzUserServiceImpl implements CmfzUserService {
    @Autowired
    private CmfzUserMapper cum;

    @Override
    public Page<CmfzUser> selectAllByNameAndTelPage(String username, String tel, Integer page, Integer limit) {
        Page<CmfzUser> page1 = new Page<>(page, limit);
        //创建条件选择器
        QueryWrapper<CmfzUser> userWrapper = new QueryWrapper<>();
        if(username!=null && !username.equals("")){
             userWrapper.like("user_name", username);
        }
        if (tel!=null && !tel.equals("")){
            userWrapper.like("user_telphone",tel);
        }
        //分页查询
        Page<CmfzUser> pi = cum.selectPage(page1, userWrapper);
        return pi;
    }

    @Override
    @LogAnnotation(type = "update",value = "修改用户")
    public void updateUser(CmfzUser u) {
        cum.updateById(u);
    }

    @Override
    @LogAnnotation(type = "insert",value = "添加用户")
    public void insertUser(CmfzUser u) {
        cum.insert(u);
    }

    @Override
    public CmfzUser selectById(Integer userId) {
        return cum.selectById(userId);
    }

    @Override
    public List<Map> selectBySexCountByMap() {
        return cum.selectBySexCountByMap();
    }

    @Override
    public List<Map> selectByProCountByMap() {
        return cum.selectByProCountByMap();
    }

    @Override
    public List<Map> selectByDateByMap() {
        return cum.selectByDateByMap();
    }

    @Override
    @AddCacheAnnotation
    public Map selectAllCount() {
        Map map = new HashMap();
        map.put("sex",selectBySexCountByMap());
        map.put("week",selectByDateByMap());
        map.put("province",selectByProCountByMap());
        return map;
    }

    /**
     *多线程
     */
    @Override
    @AddCacheAnnotation
    public Map selectAllCountThread() throws InterruptedException {
        Map map = new HashMap();
        //1.创建计数器
        CountDownLatch latch = new CountDownLatch(3);
        //2.通过线程池执行三个任务
        ThreadPoolExecutor executor = ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("sex",selectBySexCountByMap());
                //计数器减一
                latch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("week",selectByDateByMap());
                //计数器减一
                latch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                map.put("province",selectByProCountByMap());
                //计数器减一
                latch.countDown();
            }
        });
        //3.让主线程池等待
        latch.await();

        return map;
    }

}
